package com.eportal.struts.action;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import com.eportal.ORM.MpayOrg;
import com.eportal.ORM.MpayOrgView;
import com.eportal.ORM.MpayShop;
import com.eportal.ORM.ShopView;
import com.eportal.ORM.UserView;
import com.eportal.core.BaseAction;
import com.eportal.core.Page;
import com.eportal.core.Util;
import com.eportal.service.AdminService;

public class ShopAction extends BaseAction{
	AdminService service;
	AdminService serviceOracle;
	private Page page;
	private String start="0";
	private String limit="30";
	private boolean success;
	private String message;
	private String shopname;
	private String shopcode;
	private String shoparea;
	private String shopbank;
	private String shoptype;
	private String shopman;
	private String shopphone;
	private String shopmanager;
	private String startDate;
	private String endDate;
	
	
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getShopmanager() {
		return shopmanager;
	}
	public void setShopmanager(String shopmanager) {
		this.shopmanager = shopmanager;
	}
	public AdminService getServiceOracle() {
		return serviceOracle;
	}
	public void setServiceOracle(AdminService serviceOracle) {
		this.serviceOracle = serviceOracle;
	}
	public String getShoparea() {
		return shoparea;
	}
	public void setShoparea(String shoparea) {
		this.shoparea = shoparea;
	}
	public String getShopbank() {
		return shopbank;
	}
	public void setShopbank(String shopbank) {
		this.shopbank = shopbank;
	}
	public String getShoptype() {
		return shoptype;
	}
	public void setShoptype(String shoptype) {
		this.shoptype = shoptype;
	}
	public String getShopman() {
		return shopman;
	}
	public void setShopman(String shopman) {
		this.shopman = shopman;
	}
	public String getShopphone() {
		return shopphone;
	}
	public void setShopphone(String shopphone) {
		this.shopphone = shopphone;
	}
	public String getShopname() {
		return shopname;
	}
	public void setShopname(String shopname) {
		this.shopname = shopname;
	}
	public String getShopcode() {
		return shopcode;
	}
	public void setShopcode(String shopcode) {
		this.shopcode = shopcode;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public AdminService getService() {
		return service;
	}
	public void setService(AdminService service) {
		this.service = service;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		if(start==null)
			this.start="0";
		else
			this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		if(limit==null)
			this.limit = "30";
		else
			this.limit = limit;
	}
	
	public String cityterminfo(){
		LOG.info("读取citylife分点");
		UserView m = (UserView) getRequest().getSession().getAttribute(
		"mpay_user");
		String tablename = m.getTableName();

		String tablename2 = tablename.replace("_", "");
		String sql = " where '1' = '1' ";
		shopphone = shopphone == null ? "" : shopphone.trim();
		if (!shopphone.equals("")) {
			sql += " and termid like '%" + shopphone + "%' ";
		}
		shopname = shopname == null ? "" : shopname.trim();
		if (!shopname.equals("")) {
			sql += " and term_nick like '%" + shopname + "%' ";
		}
		shopbank = shopbank == null?"":shopbank.trim();
		if(!shopbank.equals("")){
			sql += " and term_name = '"+shopbank+"' ";
		}
		startDate = startDate == null?"":startDate.trim();
		endDate = endDate == null?"":endDate.trim();
		if(endDate.equals("")){
			startDate = Util.getCurrentDate("yyyyMMdd");
			endDate = Util.getCurrentDate("yyyyMMdd");
		}
		sql += " and shop_id = '"+m.getShopId()+"' ";
		String ss = (String) getRequest().getSession().getAttribute(
		"termgroup");
		Hashtable table = new Hashtable();
		List ll = serviceOracle.findBySql("select ORG_TERMCDE,sum(decode(trancde,'POV',tranamt,'POF',concat('-',tranamt),'0'))/100,count(*) from "+tablename+" where TRANAMT > '0' and ORG_TERMCDE in  ("+ss+") and FST_SDRESPN = '00' and (PEPDATE between '"+startDate+"' and '"+endDate+"') group by ORG_TERMCDE ");
		for(int i = 0;i<ll.size();i++){
			Object [] obj = (Object[])ll.get(i);
			table.put(obj[0], obj[1]+"|"+obj[2]);
		}
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);

		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		// System.out.println(sql);
		List userList = service.query("from MpayOrg" + sql, start, l);
		List tmp = new ArrayList();
		for(int i = 0;i<userList.size();i++){
			MpayOrg org = (MpayOrg)userList.get(i);
			MpayOrgView view = new MpayOrgView(org);
			if(table.get(org.getTermid())!=null){
				String st [] =((String)table.get(org.getTermid())).split("\\|");
				view.setTradecount(st[1]);
				if(!st[0].equals("null"))
					view.setTradesum(st[0]);
				else
					view.setTradesum("0.00");
			}
			
			tmp.add(view);
		}
		page = new Page();
		page.setRoot(tmp);
		page.setTotalProperty(service
				.countQuery("select count(*) from MpayOrg" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}
	
	public String getTermInfo(){
		LOG.info("读取终端列表信息...");
		UserView m = (UserView) getRequest().getSession().getAttribute(
		"mpay_user");
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);
		String tablename = m.getTableName();
		String tablename2 = tablename.replace("_", "");
		String sql = " where '1' = '1' ";
		shopphone = shopphone == null ? "" : shopphone.trim();
		if (!shopphone.equals("")) {
			sql += " and termid like '%" + shopphone + "%' ";
		}
		shopname = shopname == null ? "" : shopname.trim();
		if (!shopname.equals("")) {
			sql += " and term_nick like '%" + shopname + "%' ";
		}
		shopbank = shopbank == null?"":shopbank.trim();
		if(!shopbank.equals("")){
			sql += " and term_name = '"+shopbank+"' ";
		}
		
		sql += " and shop_id = '"+m.getShopId()+"' ";
		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		// System.out.println(sql);
		List userList = service.query("from MpayOrg" + sql, start, l);
		List tmp = new ArrayList();
		for(int i = 0;i<userList.size();i++){
			MpayOrg org = (MpayOrg)userList.get(i);
			MpayOrgView view = new MpayOrgView(org);
			view.setTradecount(serviceOracle.countQuery("select count(*) from "+tablename2+" where TRANAMT > '0' and (termid = '"+org.getTermid()+"' or ORG_TERMCDE = '"+org.getTermid()+"') and FST_SDRESPN = '00' and PEPDATE = '"+Util.getCurrentDate("yyyyMMdd")+"'")+"");
			tmp.add(view);
		}
		page = new Page();
		page.setRoot(tmp);
		page.setTotalProperty(service
				.countQuery("select count(*) from MpayOrg" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}

	public String getShopInfo(){
		LOG.info("读取商户列表数据...");
		// System.out.println(userid+" "+username);
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);
		String sql = " where '1' = '1' ";
		shopname = shopname == null ? "" : shopname.trim();
		shopcode = shopcode == null ? "" : shopcode.trim();
		if (!shopcode.equals("")) {
			sql += " and shop_code like '%" + shopcode + "%' ";
		}
		if (!shopname.equals("")) {
			sql += " and shop_name like '%" + shopname + "%' ";
		}
		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		// System.out.println(sql);
		List userList = service.query("from MpayShop" + sql, start, l);
//		List tmp = new ArrayList();
//		for (int i = 0; i < userList.size(); i++) {
//			MpayShop u = (MpayShop) userList.get(i);
//			tmp.add(u.getId());
//		}
		page = new Page();
		page.setRoot(userList);
		page.setTotalProperty(service
				.countQuery("select count(*) from MpayShop" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}
	public String addShop(){
		LOG.info("新增商户....");
		MpayShop shop = new MpayShop();
		shop.setShopDate(Util.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		shop.setShopArea(shoparea);
		shop.setShopBank(shopbank);
		shop.setShopCode(shopcode);
		shop.setShopLinkman(shopman);
		shop.setShopName(shopname);
		shop.setShopPhone(shopphone);
		shop.setShopType(shoptype);
		shop.setShopManager(shopmanager);
		try {
			service.saveOrUpdate(shop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message="新增商户有误！";
			success=false;
			return SUCCESS;
		}
		message = "新增商户成功！";
		success = true;
		return SUCCESS;
	}
	
}
